package com.lyq.org.controller;

import com.lyq.org.domain.Employee;
import com.lyq.org.query.EmployeeQuery;
import com.lyq.org.service.IEmployeeService;
import com.lyq.basic.utils.PageList;
import com.lyq.basic.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author:liangyunqian
 * @Date: 2022/12/12  14:24
 * @Description:
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description = "员工的增删改查")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //增 改
    @PutMapping
    @ApiOperation("添加或修改员工")
    public R addOrUpdate(@RequestBody Employee employee){
        try {
            if (Objects.nonNull(employee.getId())) {
                employeeService.update(employee);
            }else {
                employeeService.insert(employee);
            }
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }


    //单条删除
    @ApiOperation("根据ID删除员工")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam("主键") @PathVariable("id") Long id){
        try {
            employeeService.deleteById(id);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    //批量删除
    @ApiOperation("根据IDs批量删除员工")
    @PatchMapping
    public R batchDelete( @RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }

    //根据id查询
    @ApiOperation("根据ID查询员工")
    @GetMapping("/{id}")
    public R selectById(@ApiParam("主键") @PathVariable("id") Long id){
        try {
            Employee employee = employeeService.getById(id);
            return R.me().setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //查询所有
    @GetMapping
    @ApiOperation("查询所有员工")
    public R selectById(){
        try {
            List<Employee> employees = employeeService.getAll();
            return R.me().setData(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //分页查询
    @PostMapping
    @ApiOperation("分页查询")
    public R listPage(@RequestBody EmployeeQuery employeeQuery){
        try {
            PageList<Employee> pageList = employeeService.listPage(employeeQuery);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

}
